var ClubJoinOrInviteWindow_Type = {};
ClubJoinOrInviteWindow_Type.Join = 1;
ClubJoinOrInviteWindow_Type.Invite = 2;
var ClubJoinOrInviteWindow = Window.extend({
    _ccsFileName: res.ClubJoinOrInviteWindows_json,
    _type: null,
    _clubId: null,
    _isJoin: false, //是否为魔窗快速加入俱乐部
    initWindow: function (type, clubId, isJoin) {
        this._type = type;
        this._clubId = clubId;
        this._isJoin = isJoin;
        this._nodeList['word_join_club'].setVisible(type == ClubJoinOrInviteWindow_Type.Join);
        this._nodeList['word_invite'].setVisible(type == ClubJoinOrInviteWindow_Type.Invite);
        if (type == ClubJoinOrInviteWindow_Type.Join) {
            this._nodeList['id_des'].setString('请输入亲友圈ID');
        } else if (type == ClubJoinOrInviteWindow_Type.Invite) {
            this._nodeList['id_des'].setString('请输入玩家ID');
        }
        this._nodeList['input'].setString('');
        if (this._isJoin) {
            this._nodeList['input'].setString(clubId);
        }
    },

    open: function () {
        this._initAllListeners();
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'btn_ok': '_clickOk',
            'btn_reset': '_clickReset',
            'btn_delete': '_clickDelete',
            'btn_invite': '_clickInvite',
        };
        for (var i = 0; i <= 9; i++) {
            this._nodeList[i + ''].index = i;
            this._clickListeners[i + ''] = '_clickNumber';
        }
    },

    _clickDelete: function () {
        var str = this._nodeList['input'].getString();
        str = str.substring(0, str.length - 1);
        this._nodeList['input'].setString(str);
    },

    _clickReset: function () {
        this._nodeList['input'].setString('');
    },
    _clickOk: function () {
        var inputnum = parseInt(this._nodeList['input'].getString());
        switch (this._type) {
            case ClubJoinOrInviteWindow_Type.Join:
                this._joinClub(inputnum);
                break;
            case ClubJoinOrInviteWindow_Type.Invite:
                this._inviteMember(inputnum);
                break;
        }
    },

    _clickNumber: function (target) {
        var index = target.index;
        var str = this._nodeList['input'].getString() + index;
        this._nodeList['input'].setString(str);
    },

    _joinClub: function (inputnum) {
        if (_.isNaN(inputnum)) {
            showAlert('请输入正确的群ID（数字）');
            return;
        }

        var clubs = ClubDataManager.getAllClubData();
        for (var i = 0; i < clubs.length; i++) {
            if (clubs[i]['_id'] == inputnum) {
                showAlert('您已经在ID:' + inputnum + '的亲友圈中');
                return;
            }
        }

        var name = gameData.nickname;
        name = name.replace('%', '').replace('\'', '').replace('"', '').replace('\\', '');
        network.send(2103, {
            cmd: ClubMsgCode.ApplyClub,
            club_id: inputnum,
            name: name,
            head: gameData.headimgurl
        });
        this.close();
    },

    _inviteMember: function (inputnum) {
        if (!inputnum) {
            showAlert('玩家ID不是数字!');
            return;
        }
        network.send(2103, {cmd: ClubMsgCode.InviteClub, club_id: this._clubId, obj_id: inputnum});
        this.close();
    },
});